package org.me.marble;

import java.nio.*;

import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author tungi
 */

public class InvertMarkObject extends AvailableObject{
    private FloatBuffer frontBuffer,backBuffer,blanketBuffer;
    private MyColor color_green;

    public InvertMarkObject(int id){
        super(id);
    }

    @Override
    public void effect(GameRenderer _renderer,MoveMarble _mm){
        _renderer.invert();
        _renderer.addRunnable(timerTypes.invert.ordinal(),delay);
    }

    @Override
    public void init(){
        float[] front_side={-0.2f,0.15f,-0.05f,
                            0.15f,0.15f,-0.05f,
                            0.15f,0.05f,-0.05f,

                            0.15f,0.2f,-0.05f,
                            0.3f,0.1f,-0.05f,
                            0.15f,0.0f,-0.05f,
                            
                            0.15f,0.05f,-0.05f,
                            -0.2f,0.05f,-0.05f,
                            -0.2f,0.15f,-0.05f
        };
        float[] back_side={-0.2f,0.15f,0.05f,
                           -0.2f,0.05f,0.05f,
                           0.15f,0.05f,0.05f,

                           0.15f,0.0f,0.05f,
                           0.3f,0.1f,0.05f,
                           0.15f,0.2f,0.05f,
                           
                           0.15f,0.15f,0.05f,
                           -0.2f,0.15f,0.05f,
                           0.15f,0.05f,0.05f
        };
        float[] blanket={-0.2f,0.15f,-0.05f,
                         -0.2f,0.15f,0.05f,
                         0.15f,0.15f,-0.05f,
                         0.15f,0.15f,0.05f,
                         0.15f,0.2f,-0.05f,
                         0.15f,0.2f,0.05f,
                         0.3f,0.1f,-0.05f,
                         0.3f,0.1f,0.05f,
                         0.15f,0.0f,-0.05f,
                         0.15f,0.0f,0.05f,
                         0.15f,0.05f,-0.05f,
                         0.15f,0.05f,0.05f,
                         -0.2f,0.05f,-0.05f,
                         -0.2f,0.05f,0.05f,
                         -0.2f,0.15f,-0.05f,
                         -0.2f,0.15f,0.05f
        };

        frontBuffer=BufferFactory.MakeFloatBuffer(front_side);
        backBuffer=BufferFactory.MakeFloatBuffer(back_side);
        blanketBuffer=BufferFactory.MakeFloatBuffer(blanket);

        color=new MyColor();
        color.setAmbient(new float[]{1.0f,0.0f,0.0f,1.0f});
        color.setDiffuse(new float[]{1.0f,0.0f,0.0f,1.0f});
        color.setSpecular(new float[]{0.6f,0.6f,0.6f,0.7f});
        color.setEmission(new float[]{0.1f,0.1f,0.1f,0.6f});
        color.setShininess(1.0f);

        color_green=new MyColor();
        color_green.setAmbient(new float[]{0.0f,1.0f,0.0f,1.0f});
        color_green.setDiffuse(new float[]{0.0f,1.0f,0.0f,1.0f});
        color_green.setSpecular(new float[]{0.6f,0.6f,0.6f,0.7f});
        color_green.setEmission(new float[]{0.1f,0.1f,0.1f,0.6f});
        color_green.setShininess(1.0f);
    }

    @Override
    public void draw(GL10 gl){
        gl.glPushMatrix();
            transform(gl);

            OpenGLSystem.enable(gl,OpenGLSystem.CULL_FACE | OpenGLSystem.BLEND_IF_NO_FOG);
            OpenGLSystem.enableClientStates(gl,OpenGLSystem.NORMAL_ARRAY | OpenGLSystem.VERTEX_ARRAY);

            OpenGLSystem.setMaterial(gl,color);

            OpenGLSystem.drawTriangles(gl,null,frontBuffer,frontBuffer,
                    null,9,GL10.GL_TRIANGLES);
            OpenGLSystem.drawTriangles(gl,null,blanketBuffer,blanketBuffer,
                    null,16,GL10.GL_TRIANGLE_STRIP);
            OpenGLSystem.drawTriangles(gl,null,backBuffer,backBuffer,
                    null,9,GL10.GL_TRIANGLES);
        gl.glPopMatrix();

        //draw the second arrow
        gl.glPushMatrix();
            gl.glTranslatef(0,-0.2f,0);
            transform(gl);
            gl.glRotatef(180,0,1,0);

            OpenGLSystem.setMaterial(gl,color_green);

            OpenGLSystem.drawTriangles(gl,null,frontBuffer,frontBuffer,
                    null,9,GL10.GL_TRIANGLES);
            OpenGLSystem.drawTriangles(gl,null,blanketBuffer,blanketBuffer,
                    null,16,GL10.GL_TRIANGLE_STRIP);
            OpenGLSystem.drawTriangles(gl,null,backBuffer,backBuffer,
                    null,9,GL10.GL_TRIANGLES);

            OpenGLSystem.disableClientStates(gl,OpenGLSystem.NORMAL_ARRAY | OpenGLSystem.VERTEX_ARRAY);
            OpenGLSystem.disable(gl,OpenGLSystem.CULL_FACE | OpenGLSystem.BLEND);
        gl.glPopMatrix();
    }

}
